package com.xpsoft.oa.workflow.event;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.admin.AdminContractPayment;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.admin.AdminContractPaymentService;
import com.xpsoft.oa.service.admin.AdminContractService;
import com.xpsoft.oa.service.flow.ProcessRunService;

public class AdminContractPaymentFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(AdminContractPaymentFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String adminContractId = (String)pi.getVariable("adminContractPayment.adminContract.id");
		Date formDate = (Date)pi.getVariable("adminContractPayment.formDate");
		String receiveUnit = (String)pi.getVariable("adminContractPayment.receiveUnit");
		String contactName = (String)pi.getVariable("adminContractPayment.contactName");
		String contactTel = (String)pi.getVariable("adminContractPayment.contactTel");
		String bankName = (String)pi.getVariable("adminContractPayment.bankName");
		String bankNo = (String)pi.getVariable("adminContractPayment.bankNo");
		String payType = (String)pi.getVariable("adminContractPayment.payType");
		BigDecimal contractPercentage = (BigDecimal)pi.getVariable("adminContractPayment.contractPercentage");
		BigDecimal applyPercentage = (BigDecimal)pi.getVariable("adminContractPayment.applyPercentage");
		BigDecimal applyAmount = (BigDecimal)pi.getVariable("adminContractPayment.applyAmount");
		String applyAmountBig = (String)pi.getVariable("adminContractPayment.applyAmountBig");
		String billInfo = (String)pi.getVariable("adminContractPayment.billInfo");
		
		AdminContractPayment adminContractPayment = new AdminContractPayment();
		adminContractPayment.setFormDate(formDate);
		adminContractPayment.setReceiveUnit(receiveUnit);
		adminContractPayment.setContactName(contactName);
		adminContractPayment.setContactTel(contactTel);
		adminContractPayment.setBankName(bankName);
		adminContractPayment.setBankNo(bankNo);
		adminContractPayment.setPayType(payType);
		adminContractPayment.setContractPercentage(contractPercentage);
		adminContractPayment.setApplyAmount(applyAmount);
		adminContractPayment.setApplyAmountBig(applyAmountBig);
		adminContractPayment.setApplyPercentage(applyPercentage);
		adminContractPayment.setBillInfo(billInfo);
		if(StringUtils.isNotBlank(adminContractId)){
			AdminContractService adminContractService = (AdminContractService)AppUtil.getBean("adminContractService");
			adminContractPayment.setAdminContract(adminContractService.get(Long.valueOf(adminContractId)));
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		adminContractPayment.setProcessRunId(processRun.getRunId());
		
		AdminContractPaymentService adminContractPaymentService = (AdminContractPaymentService)AppUtil.getBean("adminContractPaymentService");
		adminContractPaymentService.save(adminContractPayment);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
